/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-05-15     
 */
package com.task.controller;

import com.task.bean.Constants;
import com.task.entity.User;
import com.task.service.IFinishTasksService;
import com.task.service.IUserService;
import com.task.utils.RenderUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 接单用户控制器
 */
@Controller
public class UserController {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

	@Autowired
	IUserService userService;
	
	@Autowired
	IFinishTasksService finishTaskService;

	@RequestMapping(value = "/personalCenter.html")
	public String toUserList(ModelMap model, @RequestParam(value = "userId") String userId) {
		try {
			User user = userService.findByUserId(userId);
			Integer count = finishTaskService.getCountByUserId(userId);
			model.addAttribute("count", count);
			float reward  = 0;
			if(null !=user && null !=user.getCumulativeReward()){
				reward =  user.getCumulativeReward();
			} 
			model.addAttribute("user", user);
			model.addAttribute("reward", reward);
		} catch (Exception e) {
			LOGGER.error("find user error:{}", e);
		}

		return "/mobile/personal_center";
	}

	@ResponseBody
	@RequestMapping(value = "/findUserPage.json")
	public JSONObject findUserPage(@RequestParam(value = "page", defaultValue = Constants.DEFAULT_PAGE) int pageNum,
			@RequestParam(value = "rows", defaultValue = Constants.DEFAULT_ROWS) int rows, ServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		try {
			Map<String, Object> searchParams = WebUtils.getParametersStartingWith(request, Constants.SEARCH_PREFIX);
			Page<User> page = userService.findUserPage(searchParams, pageNum, rows);
			if (page != null) {
				jsonObject.put(Constants.ROWS, page.getContent());
				jsonObject.put(Constants.TOTAL, page.getTotalElements());
			}
		} catch (Exception e) {
			LOGGER.error("find user page error:{}", e);
		}
		return jsonObject;
	}

	@ResponseBody
	@RequestMapping(value = "/updateUser.json")
	public JSONObject updateUser(User user) {
		JSONObject resultJson = new JSONObject();
		resultJson.put(Constants.RESULT, Constants.RESULT_FAIL);
		try {
			userService.save(user);
			resultJson.put(Constants.RESULT, Constants.RESULT_SUCCESS);
			resultJson.put(Constants.MSG, "修改接单用户成功");
		} catch (Exception e) {
			LOGGER.error("updateUser error {}", e);
			resultJson.put(Constants.MSG, "修改接单用户失败，系统错误");
		}
		return resultJson;
	}

	/**
	 * <用户个人信息>
	 * 
	 * @param id
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@ResponseBody
	@RequestMapping(value = "/findUserById.json")
	public User getById(@RequestParam(value = "id", defaultValue = "") Long id) {
		try {
			if (id != null) {
				return userService.findById(id);
			}
		} catch (Exception e) {
			LOGGER.error("find user by id error : {}", e);
		}
		return null;
	}

	@RequestMapping(value = "/deleteUser.json")
	public void deleteById(@RequestParam(value = "id", defaultValue = "") Long id, HttpServletResponse response) {
		try {
			if (id != null) {
				userService.delete(id);
			}
			RenderUtil.renderText(Constants.SUCCESS, response);
		} catch (Exception e) {
			LOGGER.error("delete user error : {}", e);
			RenderUtil.renderText(Constants.FAIL, response);
		}
	}
}
